package cn.gorit.admin.controller;

import cn.gorit.admin.service.SystemService;
import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.lang.Result;
import cn.gorit.dto.system.LinkDto;
import cn.gorit.dto.system.TimeLineDto;
import cn.gorit.entity.system.Link;
import cn.gorit.entity.system.MsgBoard;
import cn.gorit.entity.system.TimeLine;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version v1.0
 * @Classname SystemController
 * @Description 调用系统后台相关模块
 * @Author CodingGorit
 * @Created on 2021/4/16 0:20
 * @IDLE IntelliJ IDEA
 **/
@RestController
@RequestMapping("/api/v1/system")
public class SystemController {

    @Autowired
    private SystemService service;

    // ==============  友情链接后端 api
    /**
     * 【后台】添加友链
     * @param link
     * @return
     */
    @PostMapping("/back/link")
    public Result<Object> saveLink(@RequestBody LinkDto link) {
        service.saveLink(link);
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg(),"");
    }

    /**
     * 【后台】删除友链
     * @return
     */
    @RequestMapping(value = "/back/link/{id}",method = RequestMethod.DELETE)
    public Result<Object> deleteLink(@PathVariable("id") Integer id) {
        service.deleteLinkById(id);
        return new Result<>(ResultEnums.DELETE_SUCCESS.getCode(),ResultEnums.DELETE_SUCCESS.getMsg(),"");
    }

    /**
     * 【后台】修改友链
     * @param link
     * @return
     */
    @PutMapping("/back/link")
    public Result<Object> updateLink(@RequestBody Link link) {
        service.updateLink(link);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg());
    }

    /**
     * 【后台】查询所有的友链接的具体信息，不作分页
     * @return
     */
    @GetMapping("/back/link")
    public Result<List<Link>> getAllLink() {
        List<Link> list = service.getLinkAll();
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),list);
    }

    /**
     * 【后台】根据 ID 查询友链
     * @param id
     * @return
     */
    @RequestMapping(value = "/back/link/{id}",method = RequestMethod.GET)
    public Result<Link> getLinkById(@PathVariable("id") Integer id) {
        Link link = service.getLinkById(id);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),link);
    }

    // =============  留言版 后端 api ============

    @PostMapping("/back/board/page")
    public Result<Page<MsgBoard>> getMsgBoardByPage(@RequestBody Page<MsgBoard> page) {
        return service.getBroadByPage(page);
    }

    // 【后台】修改留言板，更新回复
    @PutMapping("/back/board")
    public Result<Object> updateMsgBoard(@RequestBody MsgBoard msgBoard) {
        return service.updateMsgBoard(msgBoard);
    }

    // 【后台】根据 ID 查询数据
    @GetMapping("/back/board/{id}")
    public Result<MsgBoard> getMsgBoardById(@PathVariable("id") Integer id) {
        return service.getMsgBoardById(id);
    }

    // 【后台】修改 status 状态
    @PutMapping("/back/board/{status}/{id}")
    public Result<Object> updateStatus(@PathVariable("status") Integer status, @PathVariable("id") Integer id) {
        return service.updateBroadStatus(status,id);
    }


    // =============  时间轴 =============
    // 根据 ID 获取时间轴
    @GetMapping("/back/timeline/{id}")
    public Result<Object> getTimeLineById(@PathVariable("id") Integer id) {
        return service.getTimeLineById(id);
    }

    // 添加时间轴【后台】
    @PostMapping("/back/timeline")
    public Result<Object> addTimeLine(@RequestBody TimeLineDto dto) {
        return service.addTimeLine(dto);
    }

    // 获取时间轴列表 【后台】不用单独获取
    @PostMapping("/back/timeline/page")
    public Result<Page<TimeLine>> getTimeLineByPage(@RequestBody Page<TimeLine> page) {
        return service.getTimeLineByPage(page);
    }

    // 修改单个时间轴 【后台】
    @PutMapping("/back/timeline")
    public Result<Object> updateTimeLine(@RequestBody TimeLine dto) {
        return service.updateTimeLine(dto);
    }

    // 时间轴，启用 或者 关闭 【后台】  代替逻辑删除
    @PutMapping("/back/timeline/{id}/{enable}")
    public Result<Object> timelineEnabled(@PathVariable("id") Integer id, @PathVariable("enable") Integer enable) {
        return service.enableTimeLine(id,enable);
    }

    // 删除时间轴，不开发，前天禁用即可

}
